package com.example.woke;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.http.HttpMethods;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.woke.data.Datas_chouinfo;
import com.woke.method.Intentnet;
import com.woke.method.MyApp;
import com.zhongcai.online.R;

import org.apache.http.Header;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

import cn.xl.xxl.imagecache.ImageLoader;

public class ZhouchougongyiActivity extends Activity {

	private Intent intent;
	private Datas_chouinfo data;
	private EditText mEditm;
	private GridView mGridView;
	private TextView mTinfo,mTok,mTpeople,mTtitle;
	private ArrayList<String> maplist = new ArrayList<String>();
	private ImageLoader imageLoader;
	private TextView mTtime;
	private MyApp application;
	private String trim;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_zhouchougongyi);
		
		imageLoader = new ImageLoader(this);
		intview();
		
	}

	private void intview() {
		intent = getIntent();
		application = (MyApp) getApplication();
		data = (Datas_chouinfo) intent.getSerializableExtra("data");
		mEditm = (EditText) findViewById(R.id.gongyi_edit_money);
		mGridView = (GridView) findViewById(R.id.gongyi_gridview);
		mTinfo = (TextView) findViewById(R.id.gongyi_text_info);
		mTok = (TextView) findViewById(R.id.gongyi_text_ok);
		mTpeople = (TextView) findViewById(R.id.gongyi_text_people);
		mTtitle = (TextView) findViewById(R.id.gongyi_text_title);
		mTtime = (TextView) findViewById(R.id.gongyi_text_time);
		mTtitle.setText("    "+data.getProduct_name());
		mTinfo.setText(data.getProduct_content());
		mTpeople.setText("无限额(已有"+data.getTrue_money()+"位支持)");
		mTtime.setText(Intentnet.getStrTime(data.getCreate_time()));
		Myadapter adapter = new Myadapter();
		mGridView.setAdapter(adapter);
		getdatainfo();
		mTok.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				trim = ""+mEditm.getText().toString().trim();
				if(trim.equals("")){
					Toast.makeText(ZhouchougongyiActivity.this, "请输入支持金额", Toast.LENGTH_SHORT).show();
				}else{
					intent.setClass(ZhouchougongyiActivity.this, CheckpaypswActivity.class);
					intent.putExtra("paymoney", trim);
					startActivityForResult(intent, 700);
				}
			}
		});
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		if(requestCode == 700 && resultCode == 600){
			getchilddata();
		}
		
	}
	
	private void getchilddata() {
		if(application.getDatas_load ()==null)return;
		AsyncHttpClient ahc = new AsyncHttpClient();
		RequestParams params = new RequestParams();
		params.put("g", "api");
		params.put("m", "crowd");
		params.put("a", "buyCrowd");
		params.put("crowdid", data.getId());
		params.put("user_id", application.getDatas_load().getId());
		params.put("itemid", "");
		params.put("num", "");
		params.put("money", trim);
		params.put("phone", "");
		params.put("name", "");
		params.put("code", "");
		params.put("address", "");
		ahc.get(HttpMethods.BASEURL, params, new JsonHttpResponseHandler(){

			@Override
			public void onSuccess(int statusCode, Header[] headers,
					JSONObject response) {
				super.onSuccess(statusCode, headers, response);
				Log.e("发布", ""+response);
				try {
					String flag = response.getString("flag");
					String string = response.getString("info");
					if(flag.equals("success")){
						Toast.makeText(ZhouchougongyiActivity.this, string, Toast.LENGTH_SHORT).show();
						finish();
					}else{
						Toast.makeText(ZhouchougongyiActivity.this, string, Toast.LENGTH_SHORT).show();
					}
				} catch (JSONException e) {
					e.printStackTrace();
				}
			}
			
			@Override
			public void onStart() {
				super.onStart();
			}
			@Override
			public void onFailure(int statusCode, Header[] headers,
					String responseString, Throwable throwable) {
				super.onFailure(statusCode, headers, responseString, throwable);
			}
			@Override
			public void onFinish() {
				super.onFinish();
			}
		});
	}
	
	private void getdatainfo() {
		AsyncHttpClient ahc = new AsyncHttpClient();
		RequestParams params = new RequestParams();
		params.put("g", "api");
		params.put("m", "crowd");
		params.put("a", "crowdDetail");
		params.put("crowdid", data.getId());
		ahc.get(HttpMethods.BASEURL, params, new JsonHttpResponseHandler(){

			@Override
			public void onSuccess(int statusCode, Header[] headers,
					JSONObject response) {
				super.onSuccess(statusCode, headers, response);
				
				Log.e("公益详情", ""+response);
				try {
					String flag = response.getString("flag");
					String info = response.getString("info");
					if(flag.equals("success")){
						JSONObject object = response.getJSONObject("data");
						String string = object.getString("data");
						Datas_chouinfo jsonObject = com.alibaba.fastjson.JSONObject.parseObject(string, Datas_chouinfo.class);
						maplist.add(jsonObject.getProduct_img());
					}else{
						Toast.makeText(ZhouchougongyiActivity.this, "获取数据失败！", Toast.LENGTH_SHORT).show();
					}
				} catch (JSONException e) {
					e.printStackTrace();
				}
			}
			
			@Override
			public void onStart() {
				// TODO Auto-generated method stub
				super.onStart();
			}
			
			@Override
			public void onFailure(int statusCode, Header[] headers,
					String responseString, Throwable throwable) {
				super.onFailure(statusCode, headers, responseString, throwable);
				Log.e("response", "失败"+responseString);
			}
			
			@Override
			public void onFinish() {
				super.onFinish();
//				Log.e("response", "���");
			}
		});
	}
	
	class Myadapter extends BaseAdapter{

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return maplist.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return 0;
		}
		
		class Holder {
			ImageView imgesinle;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			Holder holder = null;
			if(convertView == null){
				holder = new Holder();
				convertView = getLayoutInflater().inflate(R.layout.image_single, null);
				holder.imgesinle = (ImageView)convertView.findViewById(R.id.imageView_singel);
				convertView.setTag(holder);
			}else{
				 holder = (Holder) convertView.getTag();
			}
			imageLoader.DisplayImage("http://"+maplist.get(position), holder.imgesinle, false, false, false,0);
			return convertView;
		}
		
	}
	
}
